package dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Emp;
import util.DBUtil;

public class EmpDao implements Serializable{
	
	/**
	 * 增加一个员工
	 */
	public void save(Emp emp){
		Connection conn=null;
		try {
			
			conn=DBUtil.getConnection();
			String sql="insert into emps_pw values("
					+ "emps_seq_pw.nextval,"
					+ "?,?,?,?,?,?,?)";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, emp.getEname());
			ps.setString(2, emp.getJob());
			ps.setInt(3, emp.getMgr());
			ps.setDate(4, emp.getHiredate());
			ps.setDouble(5, emp.getSal());
			ps.setDouble(6, emp.getComm());
			ps.setInt(7, emp.getDeptno());
			
			ps.executeUpdate();
		
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("添加员工失败！",e);
		}finally{
			DBUtil.close(conn);
		}
	}
	
	
	/**
	 * 根据ID修改员工
	 */
	public void update(Emp emp){
		
		Connection conn=null;
		try {
			conn=DBUtil.getConnection();
			
			String sql="update emps_pw set "
					+ "ename=?,"
					+ "job=?,"
					+ "mgr=?,"
					+ "hiredate=?,"
					+ "sal=?,"
					+ "comm=?,"
					+ "deptno=? "
					+ "where empno=?";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, emp.getEname());
			ps.setString(2, emp.getJob());
			ps.setInt(3, emp.getMgr());
			ps.setDate(4, emp.getHiredate());
			ps.setDouble(5, emp.getSal());
			ps.setDouble(6, emp.getComm());
			ps.setInt(7, emp.getDeptno());
			ps.setInt(8, emp.getEmpno());
			
			ps.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("修改员工失败",e);
		}finally{
			DBUtil.close(conn);
		}
		
	}
	

	/**
	 * 根据ID删除一个员工
	 */
	public void delete(int id){
		Connection conn=null;
		
		try {
			conn=DBUtil.getConnection();
			String sql="delete from emps_pw "
					+ "where empno=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
		
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("删除员工失败！",e);
		}finally{
			DBUtil.close(conn);
		}
		
		
	}
	
	
	/**
	 * 根据ID查询一个员工 
	 */
	public Emp findById(int id){
		
		Connection conn=null;
		try {
			conn=DBUtil.getConnection();
			String sql="select * from emps_pw "
					+ "where empno=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			if(rs.next()){
				Emp e=new Emp();
				e.setEname(rs.getString("ename"));
				e.setJob(rs.getString("job"));
				e.setMgr(rs.getInt("mgr"));
				e.setHiredate(rs.getDate("hiredate"));
				e.setSal(rs.getDouble("sal"));
				e.setComm(rs.getDouble("comm"));
				e.setDeptno(rs.getInt("deptno"));
				e.setEmpno(rs.getInt("empno"));
				
				return e;
			}
		
		
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("查询员工失败",e);
		}finally{
			DBUtil.close(conn);
		}
		
		
		return null;
		
	}
	
	
	
	/**
	 * 根据部门ID查询员工
	 * @param deptno
	 * @return
	 */
	public List<Emp> findByDept(int deptno){
		
		List<Emp> list=new ArrayList<Emp>();
		Emp emp=null;
		Connection conn=null;
		try {
			conn=DBUtil.getConnection();
			String sql="select *from emps_pw "
					+ "where deptno=?";
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, deptno);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				int empno=rs.getInt("empno");
				String ename = rs.getString("ename");
				String job=rs.getString("job");
				int mgr=rs.getInt("mgr");
				Date hiredate=rs.getDate("hiredate");
				double sal=rs.getDouble("sal");
				double comm=rs.getDouble("comm");
				emp=new Emp(empno,ename,job,mgr,hiredate,sal,comm,deptno);
				
				list.add(emp);
			}
			
			return list;
			
			
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("查询部门员工失败！",e);
		}finally{
			DBUtil.close(conn); 
		}
		
	}
	
}
